#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'
from .core._yamldatatodisk import YamlDataToDisk
import pymel.core as pm

class ShaderMappingToDiskYaml(YamlDataToDisk):

    def scanScene(self):
        """
        Get shadingSG maps from maya file.
        :return: The dict to describe the relationship between geometries with
        shaders, e.g.
        {'lambert3SG': ['|pCube3.f[4]', '|pCube2|pCubeShape2'],
         'lambert2SG': ['|pCube1|pCubeShape1']}
        :rtype: dict
        """
        shadingMaps = {}
        defaultShadingEngines = ['initialParticleSE', 'initialShadingGroup']
        shadingEngines = pm.ls(type='shadingEngine', l=True)
        for shadingEngine in shadingEngines:
            if shadingEngine in defaultShadingEngines:
                continue
            members = []
            if pm.sets(shadingEngine, q=1):
                for i in pm.sets(shadingEngine, q=1):
                    members.append(i)
                if not members:
                    continue
                sets = []
                for member in members:
                    if '.f[' in member.name():
                        meshName = member.node().longName().replace(member.namespace(), '')
                        member = '{}.f[{}]'.format(meshName, member.index())
                    else:
                        member = member.longName().replace(member.namespace(), '')
                    sets.append(member)
                if not sets:
                    continue
                else:
                    shadingMaps.update({shadingEngine.name(): sets})
        return shadingMaps
